{
 "metadata": {
  "name": "",
  "signature": "sha256:5c37685dd25e9b361395f614fef772aacee8749413b5679891eac1538d948fc7"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#%pylab inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import cv2, Leap, math, ctypes\n",
      "import numpy as np"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 24
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "controller = Leap.Controller()\n",
      "controller.set_policy_flags(Leap.Controller.POLICY_IMAGES)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 25
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "frame = controller.frame()\n",
      "image = frame.images[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 26
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "image.is_valid"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 27,
       "text": [
        "False"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "if image.is_valid:\n",
      "    if not maps_initialized:\n",
      "        left_coordinates, left_coefficients = convert_distortion_maps(frame.images[0])\n",
      "        right_coordinates, right_coefficients = convert_distortion_maps(frame.images[1])\n",
      "        maps_initialized = True\n",
      "\n",
      "    undistorted_left = undistort(image, left_coordinates, left_coefficients, 400, 400)\n",
      "    undistorted_right = undistort(image, right_coordinates, right_coefficients, 400, 400)\n",
      "\n",
      "    #display images\n",
      "    cv2.imshow('Left Camera', undistorted_left)\n",
      "    cv2.imshow('Right Camera', undistorted_right)\n",
      "\n",
      "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
      "        break"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import cv2, Leap, math, ctypes\n",
      "import numpy as np\n",
      "\n",
      "def convert_distortion_maps(image):\n",
      "\n",
      "    distortion_length = image.distortion_width * image.distortion_height\n",
      "    xmap = np.zeros(distortion_length/2, dtype=np.float32)\n",
      "    ymap = np.zeros(distortion_length/2, dtype=np.float32)\n",
      "\n",
      "    for i in range(0, distortion_length, 2):\n",
      "        xmap[distortion_length/2 - i/2 - 1] = image.distortion[i] * image.width\n",
      "        ymap[distortion_length/2 - i/2 - 1] = image.distortion[i + 1] * image.height\n",
      "\n",
      "    xmap = np.reshape(xmap, (image.distortion_height, image.distortion_width/2))\n",
      "    ymap = np.reshape(ymap, (image.distortion_height, image.distortion_width/2))\n",
      "\n",
      "    #resize the distortion map to equal desired destination image size\n",
      "    resized_xmap = cv2.resize(xmap,\n",
      "                              (image.width, image.height),\n",
      "                              0, 0,\n",
      "                              cv2.INTER_LINEAR)\n",
      "    resized_ymap = cv2.resize(ymap,\n",
      "                              (image.width, image.height),\n",
      "                              0, 0,\n",
      "                              cv2.INTER_LINEAR)\n",
      "\n",
      "    #Use faster fixed point maps\n",
      "    coordinate_map, interpolation_coefficients = cv2.convertMaps(resized_xmap,\n",
      "                                                                 resized_ymap,\n",
      "                                                                 cv2.CV_32FC1,\n",
      "                                                                 nninterpolation = False)\n",
      "\n",
      "    return coordinate_map, interpolation_coefficients"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def undistort(image, coordinate_map, coefficient_map, width, height):\n",
      "    destination = np.empty((width, height), dtype = np.ubyte)\n",
      "\n",
      "    #wrap image data in numpy array\n",
      "    i_address = int(image.data_pointer)\n",
      "    ctype_array_def = ctypes.c_ubyte * image.height * image.width\n",
      "    # as ctypes array\n",
      "    as_ctype_array = ctype_array_def.from_address(i_address)\n",
      "    # as numpy array\n",
      "    as_numpy_array = np.ctypeslib.as_array(as_ctype_array)\n",
      "    img = np.reshape(as_numpy_array, (image.height, image.width))\n",
      "\n",
      "    #remap image to destination\n",
      "    destination = cv2.remap(img,\n",
      "                            coordinate_map,\n",
      "                            coefficient_map,\n",
      "                            interpolation = cv2.INTER_LINEAR)\n",
      "\n",
      "    #resize output to desired destination size\n",
      "    destination = cv2.resize(destination,\n",
      "                             (width, height),\n",
      "                             0, 0,\n",
      "                             cv2.INTER_LINEAR)\n",
      "    return destination"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}